package ex4.deepclone;

/**
 * @author: knowno
 * @create: 2024-09-06 14:09
 * @Version 1.0
 *
 * 1.  implements  Cloneable
 * 2. protected Object clone() 重写
 **/
public class Boy implements  Cloneable {
    private String name;
    private int age;

    //给这个boy找个女朋友
    private Girl girl;

    public Girl getGirl() {
        return girl;
    }

    public void setGirl(Girl girl) {
        this.girl = girl;
    }

    @Override
    public String toString() {
        return "Boy{" +
                "name='" + name + '\'' +
                ", age='" + age + '\'' +
                '}';
    }

    public Boy() {
    }

    public Boy(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        //这里要实现深克隆
//        boy和girl单独克隆的操作
        Boy boy = (Boy) super.clone();
        //girl.clone直接复制给boy属性girl
        boy.girl = (Girl) girl.clone();
        return boy;
    }
}
